<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\admin\model;
use think\Model;
use think\facade\Db;

class AuthGroup extends Model{
	// protected $auto = ['edittime','close_time','upgrade','discount'];
	// protected $insert = ['addtime'];

	//折扣率
    protected function setDiscountAttr(){
	    $discount_rate_arr = input('discount_rate/a');
	    $discount_tabledir_arr = input('discount_tabledir/a');
	    $discount = '';
	    if(!empty($discount_rate_arr)){
		    foreach ($discount_rate_arr as $i => $discount_rate) {
		    	$discount .= $discount_tabledir_arr[$i].':'.$discount_rate.'@';
		    }
		    $discount = rtrim($discount,'@');
	    }
		return $discount;
    }

	//有效期：截止时间
    protected function setCloseTimeAttr(){
    	$close_time = input('close_time');
    	//超过2038年的时间处理
        $datetime = new \DateTime($close_time.' 23:59:59');
        $close_time = $datetime->format('U');
        if(input('upgrade/a') == ''){
        	$close_time = null;
        }
        return $close_time;
    }

    //添加时间
    protected function setAddtimeAttr(){
		return time();
    }

    //修改时间
    protected function setEdittimeAttr(){
		return time();
    }

    //升级方式
    protected function setUpgradeAttr(){
	    $upgrade_arr = input('upgrade/a');
	    $upgrade = '';
	    if(is_array($upgrade_arr)){
		    foreach ($upgrade_arr as $val) {
		    	$upgrade .= $val.',';
		    }
		    $upgrade = rtrim($upgrade,',');
	    }
		return $upgrade;
    }

	//优惠券
    protected function setGiveCouponAttr(){
	    $give_coupon_arr = input('give_coupon/a');
	    $give_coupon = '';
	    foreach ($give_coupon_arr as $val) {
	    	$give_coupon .= $val.',';
	    }
	    $give_coupon = rtrim($give_coupon,',');
		return $give_coupon;
    }

	//权限值
    protected function setRulesAttr(){
	    $rules = implode(',',input('rules/a'));
		return $rules;
    }
	
    //新增编辑
	static function edit($id){
	    if($id>0){//编辑
			$authGroup = AuthGroup::find($id);
		}else{//新增
			$authGroup = new AuthGroup;
		}
		$authGroup->save(input('post.'));
		return $authGroup->id;
	}
	
    /**列表
    * @lang 语言
    * @order 排序
    */
	static function index($lang,$keyword,$order){
		$where[] = ['lang','=',$lang];
		$pageParam = array('lang'=>$lang);
        $pageParam += array('keyword'=>$keyword);
        
		$rs = Db::name('AuthGroup')->where($where)->where('title','like',"%$keyword%")->order($order)
			->paginate([
            	'list_rows'=> 20,
            	'query'=> $pageParam,
            ]);
		return $rs;
	}
}